let str = ''
let classifyStr = ''
let titleid
let uu = document.querySelector('.main>ul')
let lis = document.querySelectorAll('.main>ul>li')
fetch('http://chst.vip:1234/api/getbrandtitle')
    .then(body => body.json())
    .then(res => {
        let ids = []
        res.result.forEach(item => {
            titleid = item.brandTitleId
            ids.push(titleid)
            str += `<li titleid=${titleid}>
            <div class='title'>
            <span>${item.brandTitle}</span>
            <b>▼</b>
            </div>
            <div class='container'></div>
            
        </li>`
        })
        uu.innerHTML = str
        console.log(uu)
        return ids
    })
    .then(async ids => {
        let container = document.querySelectorAll(".container")

        let brand = ids.map(titleid => {
            return fetch("http://chst.vip:1234/api/getbrand?brandtitleid=" + titleid)
                .then(body => body.json())
                .then(res1 => {
                    let str = ''
                    res1.result.forEach((item, index) => {
                        str += `<div class='box' brandtitleid=${item.brandTitleId} brandName=${item.brandName}>
                    <i>${index}</i>
                    ${item.brandName}
                    <div class="count">${item.brandInfo}</div>
                    </div>`
                    })
                    return str
                })
        })
        let res = await Promise.all(brand)
        res.forEach((item, index) => {
            container[index].innerHTML = item
        })
        $('.main>ul').find('.container').prop('flag', false)
        $(".main>ul>li>div:nth-of-type(1)").click(function () {
            let flag = $(this).parent().find('.container').prop('flag')
            if (flag) {
                $(this).parent().find('.container').slideUp(200).prop('flag', false)
                return
            }
            $(this).parent().find('.container').slideDown(200).prop('flag', true).parent().siblings().find('.container').slideUp(200).prop('flag', false)
        })
        $('.main>ul>li>.container>.box').click(function () {
            let brandtitleid = $(this).attr('brandtitleid')
            let brandName = $(this).attr('brandName')
            location.href = './brandList.html?pagesize=10&brandtitleid=' + brandtitleid + '&brandName=' + brandName
        })
    })
